package com.unbox.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.unbox.admin.entity.Apps;
import com.unbox.admin.mapper.AppsMapper;
import com.unbox.admin.service.IAppsService;
import com.unbox.common.table.UnBoxPage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * < 应用管理业务实现类
 * </p>
 *
 * @author 王振宇
 * @since 2021-05-13
 */
@Service
public class AppsServiceImpl extends ServiceImpl<AppsMapper, Apps> implements IAppsService {

    @Override
    public Apps get(Apps apps) {
        LambdaQueryWrapper<Apps> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(apps.getAppId())) {
            lambdaQueryWrapper.eq(Apps::getAppId, apps.getAppId());
        }
        if (!StringUtils.isEmpty(apps.getName())) {
            lambdaQueryWrapper.eq(Apps::getName, apps.getName());
        }
        if (!StringUtils.isEmpty(apps.getAppKey())) {
            lambdaQueryWrapper.eq(Apps::getAppKey, apps.getAppKey());
        }
        if (!StringUtils.isEmpty(apps.getAppToken())) {
            lambdaQueryWrapper.eq(Apps::getAppToken, apps.getAppToken());
        }
        if (!ObjectUtils.isEmpty(apps.getStats())) {
            lambdaQueryWrapper.eq(Apps::getStats, apps.getStats());
        }
        if (!ObjectUtils.isEmpty(apps.getCreateTime())) {
            lambdaQueryWrapper.eq(Apps::getCreateTime, apps.getCreateTime());
        }
       return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<Apps> getList(Apps apps) {
        LambdaQueryWrapper<Apps> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(apps.getAppId())) {
            lambdaQueryWrapper.eq(Apps::getAppId, apps.getAppId());
        }
        if (!StringUtils.isEmpty(apps.getName())) {
            lambdaQueryWrapper.like(Apps::getName, apps.getName());
        }
        if (!StringUtils.isEmpty(apps.getAppKey())) {
            lambdaQueryWrapper.like(Apps::getAppKey, apps.getAppKey());
        }
        if (!StringUtils.isEmpty(apps.getAppToken())) {
            lambdaQueryWrapper.eq(Apps::getAppToken, apps.getAppToken());
        }
        if (!ObjectUtils.isEmpty(apps.getStats())) {
            lambdaQueryWrapper.eq(Apps::getStats, apps.getStats());
        }
        if (!ObjectUtils.isEmpty(apps.getCreateTime())) {
            lambdaQueryWrapper.eq(Apps::getCreateTime, apps.getCreateTime());
        }
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public UnBoxPage<List<Apps>> getPageList(Apps apps, int pageNo, int pageSize) {
        LambdaQueryWrapper<Apps> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(apps.getAppId())) {
            lambdaQueryWrapper.eq(Apps::getAppId, apps.getAppId());
        }
        if (!StringUtils.isEmpty(apps.getName())) {
            lambdaQueryWrapper.like(Apps::getName, apps.getName());
        }
        if (!StringUtils.isEmpty(apps.getAppKey())) {
            lambdaQueryWrapper.like(Apps::getAppKey, apps.getAppKey());
        }
        if (!StringUtils.isEmpty(apps.getAppToken())) {
            lambdaQueryWrapper.eq(Apps::getAppToken, apps.getAppToken());
        }
        if (!ObjectUtils.isEmpty(apps.getStats())) {
            lambdaQueryWrapper.eq(Apps::getStats, apps.getStats());
        }
        if (!ObjectUtils.isEmpty(apps.getCreateTime())) {
            lambdaQueryWrapper.eq(Apps::getCreateTime, apps.getCreateTime());
        }
        lambdaQueryWrapper.orderByDesc(Apps::getCreateTime);
        Page<Apps> page = new Page<>(pageNo,pageSize);
        IPage<Apps> pageResult = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        UnBoxPage<List<Apps>> unBoxPage = new UnBoxPage<>();
        unBoxPage.setPageNo(pageNo);
        unBoxPage.setPageSize(pageSize);
        unBoxPage.setSumPageCount(pageResult.getPages());
        unBoxPage.setSumDataCount(pageResult.getTotal());
        unBoxPage.setDatas(pageResult.getRecords());
        return unBoxPage;
    }

    @Override
    public List<Apps> getIdsList(List<Integer> ids) {
        LambdaQueryWrapper<Apps> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Apps::getAppId, ids);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(Apps entity) {
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(List<Apps> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(Apps apps) {
        LambdaQueryWrapper<Apps> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Apps::getAppId, apps.getAppId());
        return super.update(apps, lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatchById(List<Apps> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean remove(Apps apps) {
        LambdaQueryWrapper<Apps> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Apps::getAppId, apps.getAppId());
        return super.remove(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> idList) {
        return super.removeByIds(idList);
    }
}
